package com.lazycat.backend.service.impl.chat;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lazycat.backend.mapper.PersonalMessagesMapper;
import com.lazycat.backend.pojo.PersonalMessages;
import com.lazycat.backend.service.chat.GetMessageDoubleEndService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class GetMessageDoubleEndServiceImpl implements GetMessageDoubleEndService {

    @Autowired
    private PersonalMessagesMapper personalMessagesMapper;

    @Override
    public HashMap<String, Object> get(Integer userId1, Integer userId2) {
        QueryWrapper<PersonalMessages> personalMessageQueryWrapper = new QueryWrapper<>();
        personalMessageQueryWrapper.eq("sender_id", userId1).eq("receiver_id", userId2).orderByDesc("time");
        ArrayList<JSONObject> items = new ArrayList<>();
        List<PersonalMessages> personalMessagesA = personalMessagesMapper.selectList(personalMessageQueryWrapper);
//        for (PersonalMessages personalMessage : personalMessages) {
//            //System.out.println("1");
//            Integer sId = personalMessage.getSenderId();
//            Integer rid = personalMessage.getReceiverId();
//            String message = personalMessage.getMessage();
//            BigInteger time = personalMessage.getTime();
//            JSONObject item = new JSONObject();
//            item.put("sender_id", sId);
//            item.put("receiver_id", rid);
//            item.put("message", message);
//            item.put("time", time);
//            items.add(item);
//        }

        QueryWrapper<PersonalMessages> _personalMessageQueryWrapper = new QueryWrapper<>();
        _personalMessageQueryWrapper.eq("sender_id", userId2).eq("receiver_id", userId1).orderByDesc("time");
        List<PersonalMessages> personalMessagesB = personalMessagesMapper.selectList(_personalMessageQueryWrapper);

        //MergeSort
        ArrayList<PersonalMessages> msg = new ArrayList<>();
        int i = 0, j = 0;
        while(i < personalMessagesA.size() && j < personalMessagesB.size()) {
            PersonalMessages msgA = personalMessagesA.get(i);
            PersonalMessages msgB = personalMessagesB.get(j);
            if (msgA.getTime().compareTo(msgB.getTime()) >= 0) {
                msg.add(msgA);
                i++;
            } else {
                msg.add(msgB);
                j++;
            }
        }

        while(i < personalMessagesA.size()) {
            PersonalMessages msgA = personalMessagesA.get(i);
            msg.add(msgA);
            i++;
        }

        while(j < personalMessagesB.size()) {
            PersonalMessages msgB = personalMessagesB.get(j);
            msg.add(msgB);
            j++;
        }

        for (PersonalMessages personalMessage : msg) {
            Integer sId = personalMessage.getSenderId();
            Integer rid = personalMessage.getReceiverId();
            String message = personalMessage.getMessage();
            BigInteger time = personalMessage.getTime();
            JSONObject item = new JSONObject();
            item.put("sender_id", sId);
            item.put("receiver_id", rid);
            item.put("message", message);
            item.put("time", time);
            items.add(item);
        }

        HashMap<String, Object> resp = new HashMap<>();
        resp.put("error_message", "success");
        resp.put("messages", items);
        System.out.println(items);
        return resp;

    }
}
